﻿<script>

    $(function () {
        var _vm = new ViewModel();
        _vm.Init();
    });

    function ViewModel() {
        var me = this;
        //注意顺序
        me.DataTable = new PagedDataTable(me, "#table");
        me.DataTable.ShipObjs.push(@this.Raw(this.ViewBag.DepartmentListString));
        me.DataTable.ShipObjs.push(@this.Raw(this.ViewBag.DutyListString));
        me.DataTable.ShipObjs.push(@this.Raw(this.ViewBag.RoleListString));
        //下拉联动
        var mixinsobj = {
            data: {
                AllDutys:@this.Raw(this.ViewBag.DutyListString),//所有岗位
                Depts:@this.Raw(this.ViewBag.DepartmentListString), //部门
                Roles:@this.Raw(this.ViewBag.RoleListString),//角色
                Dutys: @this.Raw(this.ViewBag.DutyListString),//岗位
            },
            methods: {
                DeptChange: function (id) {
                    var departmentDutyList = this.AllDutys.where(function (a) {
                        return a.DepartmenId == id;
                    });
                    if (departmentDutyList && departmentDutyList.length > 0) {
                        this.Dutys = departmentDutyList;
                    }

                    else this.Dutys = DutyList = departmentDutyList;
                },
                ChangeTime: function () {

                    this.$data.Model.Birthday = $("#Birthday").val();
                }
            },

        }
        me.Dialog = new Dialog(me, "#frmEdit", mixinsobj);
        me.DialogPWD = new Dialog(me, "#frmpwd");
        me.app = new App(me, "#app");
        me.app.DeleteUrl = "@this.Href("~/SystemManage/User/Delete")";
        Extend.call(me);
    }

    function Extend() {
        var me = this;

        me.ShowDialog =function (id)
        {
            if (id == 'frmpwd') {

                me.DialogPWD.Open(me.DataTable.SelectedModel, "修改密码");
            }


        }

        me.Init = function () {
            me.LoadModels();
        }
        me.LoadModels = function (page) {
            me.DataTable.SelectedModel = null;
            var data = me.app.SearchModel;
            data.page = page || 1;
            $hits.get("@this.Href("~/SystemManage/User/GetModels")", data, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
            );
        }
        me.OnOpen = function (id) {
            if (id != '#frmpwd') {
                var model = me.Dialog.Model;
                if (model.DepartmenId) {
                    me.Dialog.DeptChange(model.DepartmentId);
                }
            }
        }
        me.OnSave = function (id) {

            //修改密码
            if (id == '#frmpwd') {
                if (!$('#RevisePasswordForm').formValid()) {
                    return false;
                }
                var model = me.DialogPWD.Model;
                var pwd = model.Password;
                var data = { userId: model.Id, newPassword: pwd };
                $hits.post("@this.Href("~/SystemManage/User/RevisePassword")", data, function (result) {
                    $hits.msg(result.Msg);
                    me.Dialog.Close();
                    me.LoadModels();
                }
                );
            }
            //用户管理
            else {
                var model = me.Dialog.Model;
                if (!$('#form1').formValid()) {
                    return false;
                }
                if (me.Dialog.EditModel) {
                    $hits.post("@this.Href("~/SystemManage/User/Update")", model, function (result) {
                        $hits.msg(result.Msg);
                        me.Dialog.Close();
                        me.LoadModels();
                    }
                    );
                }
                else {
                    $hits.post("@this.Href("~/SystemManage/User/Add")", model, function (result) {
                        $hits.msg(result.Msg);
                        me.Dialog.Close();
                        me.LoadModels();
                    }
                    );
                }
            }
        }
    }
</script>
